min.dir = '/home1/99/jc152199/brt/output/optimization_with_drops/max/'
max.dir = '/home1/99/jc152199/brt/output/optimization_with_drops/min/'

min.t.files = list.files(min.dir, pattern='dropsbyfold.csv', recursive=TRUE, full.names=TRUE)
max.t.files = list.files(max.dir, pattern='dropsbyfold.csv', recursive=TRUE, full.names=TRUE)

min.sum.files = list.files(min.dir, pattern='summary', recursive=TRUE, full.names=TRUE)
max.sum.files = list.files(max.dir, pattern='summary', recursive=TRUE, full.names=TRUE)

min.finaldrops.files = list.files(min.dir, pattern='finaldrops.csv', recursive=TRUE, full.names=TRUE)
max.finaldrops.files = list.files(max.dir, pattern='finaldrops.csv', recursive=TRUE, full.names=TRUE)

max.out.sum = NULL
	
for (i in c(1:length(max.t.files))) #### The following loop summarizes for each variable, the percentage of times it was dropped (out of 5 runs), for each set of model parameters

	{
		
	t.file = read.csv(max.t.files[i],header=T)
	
	t.sum.file = read.csv(max.sum.files[i], header=T)
	
	max.out.sum = rbind(max.out.sum,t.sum.file)
	
		for (ii in c(1:nrow(t.file)))
		
			{
			
			tt.data = t.file[ii,]
			
			tt.data[c(which(tt.data[2:6]>0))+1]=1
			
			tt.count = sum(tt.data[2:6])
			
			tt.perc = tt.count/5
			
			if(ii==1) {tt.out = NULL}
			
			tt.out = rbind(tt.out,cbind(tt.data[1],tt.perc,t.sum.file$learning.rate, t.sum.file$tree.complexity,i))
			
			}
			
	if(i==1) {max.final.out=NULL}
	
	max.final.out = rbind(max.final.out,tt.out)
	
	}
	
# Done

min.out.sum = NULL
	
for (i in c(1:length(min.t.files))) #### The following loop summarizes for each variable, the percentage of times it was dropped (out of 5 runs), for each set of model parameters

	{
		
	t.file = read.csv(min.t.files[i],header=T)
	
	t.sum.file = read.csv(min.sum.files[i], header=T)
	
	min.out.sum = rbind(min.out.sum,t.sum.file)
	
		for (ii in c(1:nrow(t.file)))
		
			{
			
			tt.data = t.file[ii,]
			
			tt.data[c(which(tt.data[2:6]>0))+1]=1
			
			tt.count = sum(tt.data[2:6])
			
			tt.perc = tt.count/5
			
			if(ii==1) {tt.out = NULL}
			
			tt.out = rbind(tt.out,cbind(tt.data[1],tt.perc,t.sum.file$learning.rate, t.sum.file$tree.complexity,i))
			
			}
			
	if(i==1) {min.final.out=NULL}
	
	min.final.out = rbind(min.final.out,tt.out)
	
	}
	
# Done

max.final.out$tt.perc[which(is.na(max.final.out$tt.perc)==T)]=0 # Convert all NA's in max.final.out to '0' to help with calcs
min.final.out$tt.perc[which(is.na(min.final.out$tt.perc)==T)]=0 # Same for min.final.out

# change some column names

names(max.final.out)[3]='learning.rate'
names(max.final.out)[4]='tree.complexity'

names(min.final.out)[3]='learning.rate'
names(min.final.out)[4]='tree.complexity'

### Aggregate

max.var.ag = aggregate(max.final.out$tt.perc, by=list(varx=max.final.out$var), FUN=mean) # Summary of max.final.out

min.var.ag = aggregate(min.final.out$tt.perc, by=list(varx=min.final.out$var), FUN=mean) # Summary of min.final.out

	
	